<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>1</button>&nbsp;
    <button>2</button>&nbsp;
    <button>3</button>&nbsp;
    <button>4</button>&nbsp;
    <script>

        var arr = [];
        for(var x = 0;x<5;x++){
            arr[x] = function(){
                console.log(x,'x');
            }
        }
        console.log(arr);
        arr[2]();
        arr[3]();

        let arr2 = [];
        for(let y = 0;y<5;y++){
            arr2[y] = function(){
                console.log(y,'y');
            }
        }
        console.log(arr2,'arr2');
        arr2[2]();
        arr2[3]();


        var btns = document.getElementsByTagName('button');
        //for(var i = 0;i<btns.length;i++){
        for(let i = 0;i<btns.length;i++){
              btns[i].onclick = function(){
                alert(i+1);
              }
        }
    </script>
</body>
</html>